// 导入db模块
const db = require('../config/db');

// 收藏类
class Collect {
    // 收藏文章
    add(article_title, article_id, collect_create_time, user_id,collect_id,classify_text,article_src){
        return new Promise((resolve,reject)=>{
            let sql2 = `select user_id from db_collect where article_id = '${article_id}' and user_id = '${user_id}'`;
            db.query(sql2)
            .then(
                (rows)=>{
                    if(rows.length == 0) {
                        let sql = `insert into db_collect(article_title, article_id, collect_create_time, user_id,collect_id,classify_text,article_src) 
                        values('${article_title}','${article_id}','${collect_create_time}','${user_id}','${collect_id}','${classify_text}','${article_src}')`;

                        db.query(sql)
                        .then(
                            (result)=>{
                                // console.log(result)
                                if(result.affectedRows === 1) {
                                    resolve({
                                        code: 200,
                                        msg: '收藏文章成功。'
                                    })
                                }
                                else {
                                    resolve({
                                        code: -1,
                                        msg: '收藏文章失败。'
                                    })
                                }
                                
                            },
                            (err)=>{
                                reject({
                                    code: -1,
                                    msg: '收藏文章失败。',
                                    err 
                                })
                            }
                        )
                    }
                    else {
                        resolve({
                            code: 1,
                            msg: '该文章已经收藏了。'
                        })
                    }
                },
                (err2)=>{
                    reject({
                        code: '查询收藏文章失败。'
                    })
                }
            )
        })
    }

    // 删除收藏的文章
    remove(collect_id){
        return new Promise((resolve,reject)=>{
            let sql = `delete from db_collect where collect_id = '${collect_id}'`
            db.query(sql)
            .then(
                (result)=>{
                    // console.log(result);
                    if(result.affectedRows === 1) {
                        resolve({
                            code: 200,
                            msg: '删除收藏的文章成功。'
                        })
                    }
                    else {
                        resolve({
                            code: -1,
                            msg: '删除收藏的文章失败111。'
                        })
                    }
                   
                },
                (err)=>{
                    reject({
                        code: -1,
                        msg: '删除收藏的文章失败2222。',
                        err 
                    })
                }
            )
        })
    }

    // 获取收藏的文章立碑
    list(user_id){
        return new Promise((resolve,reject)=>{
            let sql = `select * from db_collect where user_id='${user_id}'`;
            db.query(sql)
            .then(
                (rows)=>{
                    resolve({
                        code: 200,
                        msg: '获取收藏文章列表成功。',
                        result: rows 
                    })
                },
                (err)=>{
                    reject({
                        code: -1,
                        msg: '获取收藏的文章失败。',
                        err
                    })
                }
            )
        })
    }
}

module.exports = new Collect();